package com.prig.base.controller;

import com.prig.base.DTO.input.PageKeywordSortDto;
import com.prig.base.DTO.input.RoleDto;
import com.prig.base.entity.Role;
import com.prig.base.service.RoleService;
import com.prig.base.util.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping(path = "/role")
@Api(value = "角色")
public class RoleController {

    private RoleService roleService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }
    @RequestMapping(path = "/getRoleList",method = RequestMethod.GET)
    @ApiOperation(value = "获取role列表",notes = "获取role列表接口")
    @ApiImplicitParam(name = "adminId", value = "adminId", dataType = "Long",paramType = "query")
    @RequiresPermissions("role:get")
    public Result<List<Role>> getRoleList(@RequestParam(required = false) Long adminId,
                                          PageKeywordSortDto pageKeywordSortDto
    ){

        Page<Role> list;
        if (adminId != null){
            list = roleService.getRoles(adminId,pageKeywordSortDto);
        }else{
            list = roleService.getRoles(pageKeywordSortDto);
        }
        return new Result<>(list.getContent(),list.getTotalElements());
    }


    @RequestMapping(path = "/saveRole",method = RequestMethod.POST)
    @ApiOperation(value = "保存role",notes = "保存role接口")
    @RequiresPermissions("role:update")
    public Result saveRole(@RequestBody @Valid RoleDto dto){
        Role rqRole = new Role(dto.getRoleName(),dto.getDescription());
        if (dto.getId() == 0L){
            Role role = roleService.saveRole(rqRole);
            if (role == null){
                return new Result().c400("已存在");
            }
            return new Result();
        }
        rqRole.setId(dto.getId());
        roleService.saveRole(rqRole);
        return new Result();
    }
    @RequestMapping(path = "/delete",method = RequestMethod.POST)
    @ApiOperation(value = "删除role",notes = "根据roleId删除role")
    @RequiresPermissions("role:delete")
    public Result delete(@NotBlank@ApiParam(value = "角色id",required = true)@RequestBody String ids){
        ids = ids.replaceAll("\"","");
        roleService.delete(ids);
        return new Result("删除成功!!");
    }

    @GetMapping(path = "/{id}")
    @ApiOperation(value = "获取role",notes = "获取role接口")
    @RequiresPermissions("role:get")
    public Result getRole(@PathVariable(name="id") long id){
        Role role = roleService.getRole(id);

        if (role != null){
            return new Result<>(role);
        }
        return new Result().c400("没有数据");
    }
}
